//
//  HXCeaseTableViewController.m
//  KD
//
//  Created by ifly on 16/5/16.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXCeaseTableViewController.h"
#import "HXCeaseTableViewCell.h"

@interface HXCeaseTableViewController ()
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation HXCeaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"HXCeaseTableViewCell" bundle:nil] forCellReuseIdentifier:@"HXCeaseTableViewCell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 59;
     self.tableView.tableFooterView = [[UIView alloc]init];
    //刷新控件
    self.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    [self.tableView.mj_header beginRefreshing];
}

-(void)loadData{
    self.page = 0;
    [NetworkAPI getCeaseListWithGroupID:@"-1"
                                PageNum:[NSString stringWithFormat:@"%ld",(long)self.page]
                                Success:^(HXCeaseModel *result) {
                               if ([result.code isEqualToString:@"200"]) {
                                   [SVProgressHUD showSuccessWithStatus:@"获取通知成功"];
                                   [self.tableView.mj_header endRefreshing];
                                   self.page++;
                                   [self.dataArray removeAllObjects];
                                   [self.dataArray addObjectsFromArray:result.resultlist];
                                   [self.tableView reloadData];
                                   if ([result.resultlist count] < 10) {
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                       return;
                                   }
                               }else if([result.code isEqualToString:@"400"]){
                                   [self.tableView.mj_header endRefreshing];
                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                   self.tableView.mj_footer.alpha = 1.0f;
                               }else{
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   [self.tableView.mj_header endRefreshing];
                               }
                           } failure:^(NSError *err) {
                               [self.tableView.mj_header endRefreshing];
                           }];
}

-(void)loadMoreData{
    [NetworkAPI getCeaseListWithGroupID:@"-1"
                                PageNum:[NSString stringWithFormat:@"%ld",(long)self.page]
                                Success:^(HXCeaseModel *result) {
                               if ([result.code isEqualToString:@"200"]) {
                                   if ([result.resultlist count] == 0) {//如果没有数据了
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                       return;
                                   }else if([result.resultlist count] < 10){//有数据但是没有下一页了
                                       [self.dataArray addObjectsFromArray:result.resultlist];
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                   }else{//还有更多数据
                                       self.page++;
                                       [self.dataArray addObjectsFromArray:result.resultlist];
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshing];
                                       self.tableView.mj_footer.alpha = 0;
                                   }
                               }else if([result.code isEqualToString:@"400"]){
                                   [self.tableView.mj_header endRefreshing];
                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                   self.tableView.mj_footer.alpha = 1.0f;
                               }else{
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   [self.tableView.mj_footer endRefreshing];
                               }
                           } failure:^(NSError *err) {
                               [self.tableView.mj_footer endRefreshing];
                           }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXCeaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXCeaseTableViewCell" forIndexPath:indexPath];
    cell.data = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HXCeaseListModel *data = self.dataArray[indexPath.row];
    if ([data.StateValue isEqualToString:@"2"]) {
        CTActionSheet *actionSheet = [CTActionSheet actionSheetWithTitle:@"是否已整改?" cancelButtonTitle:@"取消" destructiveButtonTitle:@"是" otherButtonTitles:@"否" HandleBlock:^(int btnIndex) {
            if (btnIndex == 0) {//整改
                [SVProgressHUD showWithStatus:@"正在修改"];
                [NetworkAPI changeCeaseWithKeyGuid:data.KeyGuid
                                     NewStateValue:@"-2" Success:^(HXBaseModel *result) {
                                         if ([result.code isEqualToString:@"200"]) {
                                             [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                                             [self.tableView.mj_header beginRefreshing];
                                         }else{
                                             [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                         }
                                     } failure:^(NSError *err) {
                                         [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                     }];

            }
        }];
        UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
        if ([window.subviews containsObject:self.view]) {
            [actionSheet showInView:self.view];
        } else {
            [actionSheet showInView:window];
        }
    }else if([data.StateValue isEqualToString:@"5"]){
        CTActionSheet *actionSheet = [CTActionSheet actionSheetWithTitle:@"是否复工?" cancelButtonTitle:@"取消" destructiveButtonTitle:@"是" otherButtonTitles:@"否" HandleBlock:^(int btnIndex) {
            if (btnIndex == 0) {//修改
                [SVProgressHUD showWithStatus:@"正在修改"];
                [NetworkAPI changeCeaseWithKeyGuid:data.KeyGuid
                                     NewStateValue:@"-5" Success:^(HXBaseModel *result) {
                                         if ([result.code isEqualToString:@"200"]) {
                                             [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                                             [self.tableView.mj_header beginRefreshing];
                                         }else{
                                             [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                         }
                                     } failure:^(NSError *err) {
                                         [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                     }];
            }
        }];
        UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
        if ([window.subviews containsObject:self.view]) {
            [actionSheet showInView:self.view];
        } else {
            [actionSheet showInView:window];
        }
    }else{
        
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

@end
